// 基础引入
import path from "path";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import { defineConfig, loadEnv } from "vite";
// 插件引入
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import customPlugins from "./plugins/index.js";
// 三方组件库
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";

export default defineConfig(({ command, mode }) => {
  const { VITE_PROJECT_NAME, VITE_API_BASE_URL, VITE_PORT } = loadEnv(mode, process.cwd());
  return {
    base: "./",
    /* 插件配置 */
    plugins: [
      vue(),
      vueJsx(),
      AutoImport({
        imports: ["vue", "vue-router", "pinia"],
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),
      // 自定义插件: 依赖包版本校验
      customPlugins.vitePluginDepVersion(),
      // 自定义插件: 页面加载动画
      customPlugins.vitePluginPageLoad({ system: VITE_PROJECT_NAME }),
      // 自定义插件: 异常错误捕获
      customPlugins.vitePluginCatchError({ system: VITE_PROJECT_NAME, baseUrl: VITE_API_BASE_URL }),
      // 自定义插件: 前端版本更新通知
      customPlugins.vitePluginWebVersion(),
    ],
    /* 路径配置 */
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "./src"),
      },
    },
    /* JSON配置 */
    json: {
      stringify: true, // JSON文件导入时, 会被自动解析成对象
    },
    /* 服务配置 */
    server: {
      open: true,
      host: "0.0.0.0", // 指定项目监听哪个IP, ['0.0.0.0' || true]将监听所有地址,包括局域网和公网
      port: VITE_PORT,
    },
  };
});
